import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.apache.*;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2020-07-16
 * Time: 14:28
 *
 * @author zyh
 */
public class ZKtest {


    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        String coonectString = "192.168.1.38:2181,192.168.1.39:2181,192.168.1.40:2181,192.168.1.41:2181";
        ZooKeeper zooKeeper = new ZooKeeper(coonectString, 2000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("zookeeper:" + event.toString());
                Event.KeeperState zookeperState = event.getState();
                switch (zookeperState) {
                    case Unknown:
                        break;
                    case Disconnected:
                        break;
                    case NoSyncConnected:
                        break;
                    case SyncConnected:
                        countDownLatch.countDown();
                        break;
                    case AuthFailed:
                        break;
                    case ConnectedReadOnly:
                        break;
                    case SaslAuthenticated:
                        break;
                    case Expired:
                        break;
                    case Closed:
                        break;
                    default:
                        break;
                }

            }
        });
        countDownLatch.await();
        String path = zooKeeper.create("/abc", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        Stat stat = new Stat();
        zooKeeper.getData("/abc", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("node:"+event.toString());
                try {
                    zooKeeper.getData("/abc",this,stat);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },stat);
        zooKeeper.setData("/abc","321".getBytes(),0);
        while (true){

        }
    }
}
